<?php

require_once ('MDB2.php');
require_once ('MDB2_QueryObject.php');
require_once ('MDB2_QueryResult.php');

class MDB2_DAO extends DAO
{

  /**
   *
   * @var MDB2_Driver_Common
   */
  private $mdb2;

  /**
   *
   * @var array
   */
  protected $dsn;

  /**
   *
   * @var array
   */
  protected $options;

  /**
   * Constructor.
   *
   * @param array $params
   * @return void
   */
  protected function __construct($params)
  {
    parent::__construct($params);

    $this->dsn = array(
      'phptype' => $this->params->get('type', 'mysql'), 'username' => $this->params->get('username'),
      'password' => $this->params->get('password'), 'hostspec' => $this->params->get('host', 'localhost'),
      'database' => $this->params->get('name'), 'charset' => $this->params->get('charset', 'utf8')
    );

    $this->options = array(
      'debug' => 2, 'portability' => MDB2_PORTABILITY_ALL
    );
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IDataAccessObject#lastInsertId()
   */
  public function lastInsertId()
  {
    return $this->db()->lastInsertID();
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/DAO#beginTransaction()
   */
  public function beginTransaction()
  {
    $this->db()->beginTransaction();
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/DAO#commit()
   */
  public function commit()
  {
    $this->db()->commit();
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/DAO#rollback()
   */
  public function rollback()
  {
    $this->db()->rollback();
  }

  /**
   *
   * @return MDB2_Driver_Common
   */
  public function db()
  {
    return $this->connect()->mdb2;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/DAO#connect()
   */
  public function connect()
  {
    if (empty($this->mdb2)) {
      $this->mdb2 = MDB2::connect($this->dsn, $this->options);

      if (PEAR::isError($this->mdb2)) {
        throw new Exception($this->mdb2->getUserInfo());
      }

      $this->mdb2->fetchmode = MDB2_FETCHMODE_ASSOC;
    }
    else {
      $this->mdb2->connect();
    }

    return $this;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/DAO#disconnect()
   */
  public function disconnect()
  {
    if ($this->mdb2 && ! PEAR::isError($this->mdb2)) {
      $this->mdb2->disconnect();
    }

    return $this;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/DAO#quote($value, $type)
   */
  public function quote($value, $type = null)
  {
    return $this->db()->quote($value, $type);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/DAO#factoryQueryObject()
   */
  public function factoryQueryObject()
  {
    return new MDB2_QueryObject($this);
  }

  /**
   * Check if $res is MDB2_Error
   *
   * @param mixed $res
   * @throws Exception if any errors ocurred
   * @return void
   */
  public static function validate($res, $info = null)
  {
    if (PEAR::isError($res)) {
      $msg = $res->getUserInfo();

      if (preg_match('/\[Native message: ([^\]]*)\]/', $msg, $o)) {
        $msg = $o[1];
      }
      else if (preg_match('/\[Error message: ([^\]]*)\]/', $msg, $o)) {
        $msg = $o[1];
      } /* else {
        $msg = $res->getMessage();
      }*/

      if (! empty($info)) {
        $info = "\n\n" . implode("\n\n", ( array ) $info);
      }

      throw new DataAccessObjectException($msg . $info);
    }
  }

}

?>